<template>
  <div class="app-page">
    <table-search
      ref="tableSearch"
      label-width="80px"
      :search-form="searchFormItem"
      :on-submit="handleQuery"
      :on-reset="handleQuery"
    />
    <el-card>
      <div class="content">
        <div class="app-table-action-wrap">
          <auth-button
            auth-key="c_1"
            type="primary"
            icon="el-icon-plus"
            size="small"
            @click="handleAdd"
          >
            新增
          </auth-button>
          <auth-button
            auth-key="c_4"
            size="small"
            @click="handleExport"
          >
            导出
          </auth-button>
        </div>

        <table-paging
          :table-list="tableList"
          :table-column="tableColumn"
          :table-btn-width="110"
          :table-paging="tablePaging"
          :table-total="tableTotal"
          @pagingChange="handlePaginChange"
        />
      </div>

      <device-update
        ref="update"
        :title="dialogTitle"
        :visible.sync="dialogVisible"
        @handleModalSubmit="handleModalSubmit"
      />
    </el-card>
  </div>
</template>

<script>
import { fetchDeviceFindPage, fetchDeviceAdd, fetchDeviceGet, fetchDeviceUpdate, fetchDeviceDelete, fetchDeviceExport } from '@/api/basicinfo/device'
import { TableSearch, TablePaging, AuthButton } from '@/components'
import { searchFormItem, tableColumn } from './config'
import DeviceUpdate from './update'

export default {
  name: 'Device',
  components: { TableSearch, TablePaging, DeviceUpdate, AuthButton },
  data() {
    return {
      searchFormData: {},
      searchFormItem,

      tableColumn: tableColumn(this.handleEdit, this.handleDelete),
      tableList: [],
      tableTotal: 0,
      tablePaging: {
        page: 1,
        pagesize: 10
      },

      dialogTitle: '新增', // 弹窗标题
      dialogVisible: false // 弹窗显示隐藏
    }
  },
  created() {
    this.postFindPage()
  },
  methods: {
    handleQuery(values) { // 查询
      this.searchFormData = values
      this.tablePaging = {
        page: 1,
        pagesize: 10
      }
      this.postFindPage()
    },

    handleAdd() { // 新增
      this._setModalData()
    },

    handleEdit(id) { // 修改
      this.postGet(id)
    },

    handleDelete(id) {
      this.postDelete(id)
    },

    handleExport() { // 导出
      fetchDeviceExport(this.$refs.tableSearch.formValue)
    },

    handleModalSubmit(values) { // 弹窗提交
      if (values.id) {
        this.postUpdate(values)
        return false
      }
      this.postAdd(values)
    },

    hanleClose() { // 弹窗关闭
      this.dialogVisible = false
    },

    handlePaginChange(pageInfo) { // 分页改变
      this.tablePaging = pageInfo
      this.postFindPage()
    },

    _setModalData(title = '新增', visible = true, modalFormData = {}) { // 设置弹窗
      this.dialogVisible = visible
      this.dialogTitle = title
      this.$refs.update.modalFormData = modalFormData
    },

    postFindPage() { // 列表
      fetchDeviceFindPage({ ...this.searchFormData, ...this.tablePaging }).then(res => {
        const { records, current, size, total } = res
        this.tableList = records
        this.tablePaging = {
          page: current,
          pagesize: size
        }
        this.tableTotal = total
      })
    },

    postAdd(values) { // 新增
      fetchDeviceAdd(values).then(res => {
        this.dialogVisible = false
        this.$message.success('新增成功')
        this.postFindPage()
      })
    },

    postGet(id) { // 详情
      fetchDeviceGet({ id }).then(res => {
        this._setModalData('修改', true, res)
      })
    },

    postUpdate(values) { // 更新
      fetchDeviceUpdate(values).then(() => {
        this.dialogVisible = false
        this.$message.success('修改成功')
        this.postFindPage()
      })
    },

    postDelete(id) { // 删除
      fetchDeviceDelete({ id }).then(() => {
        this.$message.success('删除成功')
        this.postFindPage()
      })
    }
  }
}
</script>

<style lang="scss" scoped></style>
